$(document).ready(function() {
	
	// global - login
	$("#home-login-btn").click(function(e) {
		e.preventDefault();
		$("#login-box").animate({
		  opacity: 'show',
		  height: 'toggle'
		  }, 300, function() {
		    $("#home-login").focus();
		});
	});
	
	// if click on the login box
	$("#login-box").mouseup(function() {
		return false
    });
	// if click on everything else
    $(document).mouseup(function() {
        if($("#login-box").length) {
            $("#login-box").hide();
        }
    });
	
	
	// sidebar search autofill
	$("#searchform input").focus(function() {
		if ($(this).val() == 'search...') $(this).val('');
		$(this).blur(function(){
			if ($(this).val() == '') $(this).val('search...');
		});
	});
	
	
	// tipsy tooltip (tips: general / w-tips: for form pages)
	$(".tips").tipsy({gravity: 's', trigger: 'focus', html: true});
	$(".w-tips").tipsy({trigger: 'focus', gravity: 'w', html: true});
	$(".e-tips").tipsy({gravity: 'e', html: true});
	$(".n-tips").tipsy({gravity: 'n', html: true});
	
	
	//	smart expanding area
	$('.auto-expand').autoResize({
	  animateDuration : 300,
	  extraSpace : 20
	});
	
	
	//	spam ajax colorbox popup
    $(".r-spam").colorbox();
    $(".r-promote").colorbox({'maxWidth': 600});
	$(".share-dialog").colorbox({'maxWidth': 800});
    $(".fancybox").colorbox({'maxHeight': '90%'});
    $(".colorbox").colorbox({'maxHeight': '90%'});
    $(".colorbox-if").colorbox({'innerWidth': '512','innerHeight' : '384','iframe': true});
    $("a#feedback-client").colorbox({'maxWidth': 900});
    $("a#feedback-user").colorbox({'maxWidth': 900,'inline' : true,'href' : '#feedback-form'});
    $(".ambassador-form").colorbox();
    $(".reject-form").colorbox({'innerWidth': 400});
	$(".signin-combo").colorbox();
	$("#email-share").colorbox({'innerWidth': 400});
	
	
	// help center collapsible content
	$(".help-content").hide();
	$(".help-title").live('click', function(e) {
		e.preventDefault();
		$(this).parent().next(".help-content").animate({
		  opacity: 'toggle',
		  height: 'toggle'
		  }, 300);
	});
	
	
	// bastard parent eater (close button)
	$(".btn-close").click(function(e) {
		e.preventDefault();
		$(this).parent().next(".screen-message:hidden").fadeIn();
		$(this).parent().hide();
	});
	
	
	// hide other screen-messages
	$(".screen-message").next(".screen-message").hide();


	// profile page
	$(".profile-menu .btn-profile").live('click', function(e) {
		e.preventDefault();
		$("#profile-actions").animate({
		  opacity: 'toggle',
		  height: 'toggle'
		  }, 300);
		$(this).parent().parent().find(".welcoming").toggle();
	});
	
	// master rewards list
	$("#master-reward-list li").click(function() {
	  window.location = $(this).find("a").attr("href");
	});
	
	// index list behavior
	$(".clickable").click(function() {
	  if ($(this).find("a")) {
	  	window.location = $(this).find("a").attr("href");
  	  }
	});
	
	// home reward banner
	$(".banner-ad, #wallet").click(function(){
		if($(this).find("a")) {
			rewardBanner = $(this).find("a").attr("href");
			window.location = rewardBanner;
		};
	});
	
	// wallet effect holy WOW!
	$("#wallet").hover(
		function () {
			$(this).find("p").toggle();
		}, 
		function () {
			$(this).find("p").toggle();
		}
	);

	
	// FLASHERS! Beware girls!
	if($(".flash-content").length) {
		$(".flash-content").append("<a class='flash-close' href='#close-this-notice'>close</a>");
		$("#flash").slideDown(500);
	}
	
	$(".flash-close").live('click', function(e) {
		e.preventDefault();
		$("#flash").slideUp(300);
	});
	
	
	// comment counter
	$(".comment-source").keyup(function(e) {
      diffLength = 255 - $(this).val().length;
      $(this).parents(".c-form").find(".comment-counter").text("Characters left: " + diffLength);
      if(diffLength < 0) {
        $(this).parents(".c-form").find(".comment-counter").addClass("error");
      } else {
        $(this).parents(".c-form").find(".comment-counter").removeClass("error");
      }
    });
	
	// comment validator
    $(".c-form form").submit(function(e) {
      if($(this).find(".comment-source").val().length < 256) {
        $(this).submit();
      } else {
        return false;
      }
    });	
	
	
	// Show the error
	if ($("#errorExplanation").length) $("#errorExplanation").slideDown(500);
  

	if($(".date-field").length) {
	  $(".date-field").datepicker({dateFormat: 'dd-mm-yy' });
	}

	// if($(".radio-toggle").length) {
	  $(".radio-toggle").live('click', function() {
	    	// $(".form-toggle:visible").hide();
				// $(".form-toggle:hidden").show();
				$(".form-toggle").toggle();
	  });
	// }
 
	if ($('.survey-urls input').length) {
	  $('.survey-urls input').focus(function() {
		$(this).select();
	  });
	}
	
	
	//  global check box, to use: set master checkbox's class to global-check-all, and set the wrapper's class to global-check-box
	$(".global-check-all").live('click', function(e) {
    var checked = $(this).attr('checked');
    $(".global-check-box").find(':checkbox').attr('checked', checked);
  });


  //	ADMIN: Surveys
  //
  //	to add new quota in admin/surveys/:id/edit
  $(".new_quota").click( function() {
    var new_id = new Date().getTime();
    $(this).before(quota.replace(/NEW_RECORD/g, new_id)); //replace the string NEW_RECORD with some unique identifier
    return false;
  });

  //	to delete quota in admin/surveys/:id/edit
  $(".delete_quota").click( function() {
    $(this).parent().slideUp();
    $(this).prev('input[type=hidden]').val(1);
  });
});